'use strict';

/**
 * @ngdoc function
 * @name youaBiDashboardAngularApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the youaBiDashboardAngularApp
 */
angular.module('youaBiDashboardAngularApp')
  .controller('LoginCtrl', function($scope, $http, $location) {
    this.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];

    $scope.username = null;
    $scope.password = null;

    $scope.userlogin = function() {

      if (!$scope.username || !$scope.password) return

      $http({
        method: 'POST',
        url: '/user/login',
        params: {
          username: $scope.username,
          password: md5($scope.password)
        }
      }).then(function successCallback(response) {

        var result = response.data.result;
        var error = response.data.error;

        if (error == 0) {
          window.localStorage.setItem('csrftoken', result.csrftoken);
          window.localStorage.setItem('username', result.username);

          $location.path('/')
        }

      }, function errorCallback(response) {

      });
    }
  });